﻿using System.Diagnostics;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using K8s.NET.Demo.Models;
using System.Net;
using System.Net.Sockets;
using System;

namespace K8s.NET.Demo.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        public IActionResult Index()
        {
            var hostname = Dns.GetHostName ();
            ViewBag.HostName = hostname;
            ViewBag.HostIp = Dns.GetHostAddresses(hostname)
               .FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork);
               
            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        public IActionResult Sqrt()
        {
            var x = 0.0001;
            for(double i=0; i<= 1000000; i++)
            {
                x += Math.Sqrt(i);
            }

            return Ok("OK");
        }

        public IActionResult CheckHealth () 
        {
            if (new Random().Next(100) > 25)
            {
                return Ok("OK");
            }
            else
            {
                return BadRequest();
            }
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }

    public class Test
    {
        public decimal Shipcost(decimal weight,decimal weight2)
        {
            decimal price = -1;
            if (weight >= 21)
                weight2 = Math.Ceiling(weight2/2);

            if (weight>0m && weight<=0.5m)  
                price =274m;
            else if (weight>0.5m && weight<=1m)
                price =300m;
            else if (weight>1m && weight<=1.5m)
                price =326m;
            else if (weight>1.5m && weight<=2m)
                price =352m;
            else if (weight>2m && weight<=2.5m)
                price = 377m;
            else if (weight>2.5m && weight<=3m)
                price = 403m;
            else if (weight>3m && weight<=3.5m)
                price = 429m;
            else if (weight>3.5m && weight<=4m)
                price = 455m;
            else if (weight>4m && weight<=4.5m)
                price =481m;
            else if (weight>4.5m && weight<=5m)
                price =506m;
            else if (weight>5m && weight<=5.5m)
                price =529m;
            else if (weight>5.5m && weight<=6m)
                price = 552m;
            else if (weight>6m && weight<=6.5m)
                price = 574m;
            else if (weight>6.5m && weight<=7m)
                price = 597m;
            else if (weight>7m && weight<=7.5m)
                price = 619m;
            else if (weight>7.5m && weight<=8m)
                price = 642m;
            else if (weight>8m && weight<=8.5m)
                price = 665m;
            else if (weight>8.5m && weight<=9m)
                price = 687m;
            else if (weight>9m && weight<=9.5m)
                price = 710m;
            else if (weight>9.5m && weight<=10m)
                price = 733m;
            if (weight>10m && weight<=10.5m)
                price = 752m;
            else if (weight>10.5m && weight<=11m)
                price = 771m;
            else if (weight>11m && weight<=11.5m)
                price = 791m;
            else if (weight>11.5m && weight<=12m)
                price = 810m;
            else if (weight>12m && weight<=12.5m)
                price = 829m;
            else if (weight>12.5m && weight<=13m)
                price = 849m;
            else if (weight>13m && weight<=13.5m)
                price = 868m;
            else if (weight>13.5m && weight<=14m)
                price = 887m;
            else if (weight>14m && weight<=14.5m)
                price = 907m;
            else if (weight>14.5m && weight<=15m)
                price = 926m;
            else if (weight>15m && weight<=15.5m)
                price = 946m;
            else if (weight>15.5m && weight<=16m)
                price = 965m;
            else if (weight>16.0m && weight<=16.5m)
                price = 984m;
            else if (weight>16.5m && weight<=17m)
                price = 1004m;
            else if (weight>17m && weight<=17.5m)
                price = 1023m;
            else if (weight>17.5m && weight<=18m)
                price = 1042m;
            else if (weight>18.0m && weight<=18.5m)
                price = 1062m;
            else if (weight>18.5m && weight<=19m)
                price = 1081m;
            else if (weight>19m && weight<=19.5m)
                price = 1100m;
            else if (weight>19.5m && weight<=20m)
                price = 1120m;
            else if (weight>20m && weight<=21m)
            price = 1115.6m;
            else if (weight>21m && weight<=22m)
                price = 1159.2m;

            else if (weight >= 22.5m && weight <= 30.0m)
                price = 46.6m*weight2;
            else if (weight >= 30.5m && weight <=44.0m)
                price =43.9m*weight2;
            else if (weight >= 44.5m && weight <= 74.0m)
                price = 40.9m*weight2;
            else if (weight >= 74.5m && weight <= 101.0m)
                price = 38m*weight2;
            else if (weight >= 101.5m && weight <= 250.0m)
                price = 37.8m*weight2;
            else if (weight >= 250.5m && weight <= 500.0m)
                price = 37.3m*weight2;
            else if (weight >= 500.5m )
                price = 37.2m*weight2;
            return price;
        }
    }
}
